package com.mvs.flyingbot;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;

public class World {
	public interface WorldListener {
		public void jump();

		public void highJump();

		public void hit();

		// public void coin ();
	}

	public static final float WORLD_WIDTH = 10;
	public static final float WORLD_HEIGHT = 15 * 20;
	public static final int WORLD_STATE_RUNNING = 0;
	public static final int WORLD_STATE_NEXT_LEVEL = 1;
	public static final int WORLD_STATE_GAME_OVER = 2;
	public static final Vector2 gravity = new Vector2(0, -10);

	public final Robot robot;
	public final List<Platform> platforms;
	// public final List<Coin> coins;
	public final WorldListener listener;
	public final Random rand;

	public float heightSoFar;
	public int score;
	public int state;

	public World(WorldListener listener) {
		this.robot = new Robot(5, 1.5f);
		this.platforms = new ArrayList<Platform>();
		// this.coins = new ArrayList<Coin>();
		this.listener = listener;
		rand = new Random();
		generateLevel();

		this.heightSoFar = 0;
		this.score = 0;
		this.state = WORLD_STATE_RUNNING;
	}

	private void generateLevel() {
		float y = Platform.PLATFORM_HEIGHT / 2;
		float maxJumpHeight = 7;
		while (y < WORLD_HEIGHT - WORLD_WIDTH / 2) {
			int type = rand.nextFloat() > 0.2f ? Platform.PLATFORM_TYPE_MOVING
					: Platform.PLATFORM_TYPE_STATIC;
			float x = rand.nextFloat()
					* (WORLD_WIDTH - Platform.PLATFORM_WIDTH)
					+ Platform.PLATFORM_WIDTH / 2;

			Platform platform = new Platform(type, x, y);
			platforms.add(platform);

			// if (rand.nextFloat() > 0.6f) {
			// Coin coin = new Coin(platform.position.x + rand.nextFloat(),
			// platform.position.y + Coin.COIN_HEIGHT
			// + rand.nextFloat() * 3);
			// coins.add(coin);
			// }

			y += (maxJumpHeight - 0.5f);
			y -= rand.nextFloat() * (maxJumpHeight / 3);
		}

	}

	public void update(float deltaTime, float accelX, boolean jump) {
		updateRobot(deltaTime, accelX, jump);
		updatePlatforms(deltaTime);
		// updateSquirrels(deltaTime);
		// updateCoins(deltaTime);
		if (robot.state != Robot.ROBOT_STATE_HIT)
			checkCollisions();
		checkGameOver();
	}

	private void updateRobot(float deltaTime, float accelX, boolean jump) {

		if (robot.state != Robot.ROBOT_STATE_HIT) {
			robot.velocity.x = -accelX / 10 * Robot.ROBOT_MOVE_VELOCITY;

		}
		if (jump)// &&robot.state!=robot.ROBOT_STATE_JUMP)
		{
			robot.robotJump();
			listener.jump();
		}

		robot.update(deltaTime);

	}

	private void updatePlatforms(float deltaTime) {
		int len = platforms.size();
		for (int i = 0; i < len; i++) {
			Platform platform = platforms.get(i);
			platform.update(deltaTime);
			// if (platform.state == Platform.PLATFORM_STATE_PULVERIZING &&
			// platform.stateTime > Platform.PLATFORM_PULVERIZE_TIME) {
			// platforms.remove(platform);
			// len = platforms.size();
			// }
		}
	}

	// private void updateCoins (float deltaTime) {
	// int len = coins.size();
	// for (int i = 0; i < len; i++) {
	// Coin coin = coins.get(i);
	// coin.update(deltaTime);
	// }
	// }

	private void checkCollisions() {
		checkPlatformCollisions();
		// checkItemCollisions();

	}

	private void checkPlatformCollisions() {

		int len = platforms.size();
		for (int i = 0; i < len; i++) {
			Platform platform = platforms.get(i);
			if (robot.position.y > platform.position.y
					&& platform.count != true) {
				score++;
				platform.count = true;

			}

			if (robot.bounds.overlaps(platform.bounds)) {
				robot.velocity.y = 0;
				listener.hit();
				state = WORLD_STATE_GAME_OVER;
				break;
			}

		}
	}

	// private void checkItemCollisions () {
	// int len = coins.size();
	// for (int i = 0; i < len; i++) {
	// Coin coin = coins.get(i);
	// if (bob.bounds.overlaps(coin.bounds)) {
	// coins.remove(coin);
	// len = coins.size();
	// listener.coin();
	// score += Coin.COIN_SCORE;
	// }
	//
	// }
	//

	// }
	// }
	// }
	// }

	private void checkGameOver() {
		if (heightSoFar - 7.5f > robot.position.y) {
			state = WORLD_STATE_GAME_OVER;
		}
		if (robot.position.y < 0)
			state = WORLD_STATE_GAME_OVER;
	}
}
